package com.auxgroup.hr.front.service.comm.impl;

import javax.annotation.Resource;

import com.auxgroup.hr.front.dao.domain.HrMobileAccessRecord;
import com.auxgroup.hr.front.dao.repository.HrMobileAccessRecordRepository;
import com.auxgroup.hr.front.service.comm.SysDictService;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCOprClasidVw;
import com.auxgroup.hr.front.dao.domain.PsCPersonalData;
import com.auxgroup.hr.front.dao.domain.Psoprdefn;
import com.auxgroup.hr.front.dao.repository.PsCOprClasidVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCPersonalDataRepository;
import com.auxgroup.hr.front.dao.repository.PsoprdefnRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.comm.OprdefnService;

import java.util.Date;

/**
 * @author lvliuhao
 * @version 创建时间：180523
 * 入职人员银行账号service
 */
@Service("oprdefnService")
public class OprdefnServiceImpl implements OprdefnService {


    @Autowired
    private PsoprdefnRepository psoprdefnRepository;

    @Autowired
    private PsCPersonalDataRepository psCPersonalDataRepository;

    @Autowired
    private PsCOprClasidVwRepository psCOprClasidVwRepository;
    @Autowired
    private PersonalDataService personalDataService;
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private HrMobileAccessRecordRepository hrMobileAccessRecordRepository;

    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;

    @Override
    public UserDTO getUserDto(String oprid) {
        Psoprdefn oprinfo = psoprdefnRepository.getUserByOprid(oprid);
        if (oprinfo == null) {
            return null;
        }
        UserDTO user = objectMapper.map(oprinfo, UserDTO.class, "userDtoCopy");
        PsCOprClasidVw oprClasid = psCOprClasidVwRepository.findOne(oprinfo.getOprid());
        //设置主页权限
        user.setFlag(oprClasid.getFlag() == null ? "N" : oprClasid.getFlag());
        user.setFlag1(oprClasid.getFlag1() == null ? "N" : oprClasid.getFlag1());
        user.setFlag2(personalDataService.hasPaySlipPermission(oprid) ? "Y" : "N");
        user.setFlag3(oprClasid.getFlag3() == null ? "N" : oprClasid.getFlag3());
        user.setBu(sysDictService.getBuByAccount(oprid));
        PsCPersonalData person = psCPersonalDataRepository.findOne(user.getEmplId());
        if (person != null) {
            user.setPhoneNum(person.getPhone());
        }
        HrMobileAccessRecord hrMobileAccessRecord = new HrMobileAccessRecord();
        hrMobileAccessRecord.setAccessTime(new Date());
        hrMobileAccessRecord.setAccount(user.getOprId());
        hrMobileAccessRecord.setBadge(user.getEmplId());
        hrMobileAccessRecord.setName(user.getOprdefndesc());
        hrMobileAccessRecordRepository.save(hrMobileAccessRecord);
        return user;
    }

    @Override
    public Psoprdefn getOprDefn(String oprid) {
        return psoprdefnRepository.findOne(oprid);
    }

}
